﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MsTestUtilities.Utilities
{
    public static class CollectionAsserter
    {
        public static AssertionResult AreEquivalent<T>(IEnumerable<T> expected, IEnumerable<T> actual)
        {
            var missingInActual = LeftDifference(expected, actual);
            if (missingInActual.Any())
            {
                var message = string.Format("Expected contained elements not found in Actual. Missing element(s) [{0}]", string.Join(", ", missingInActual));
                return AssertionResult.Error(message);
            }

            var extraInActual = LeftDifference(actual, expected);
            if (extraInActual.Any())
            {
                var message = string.Format("Actual contained unexpected elements. Extra element(s) [{0}]", string.Join(", ", extraInActual));
                return AssertionResult.Error(message);
            }

            if (expected.Count() != actual.Count())
            {
                var message = string.Format(
                    "Actual contained a different number of elements than expected. Expected {0}. Actual {1}.",
                    expected.Count(),
                    actual.Count());

                return AssertionResult.Error(message);
            }

            return AssertionResult.Success;
        }

        public static IEnumerable<T> LeftDifference<T>(IEnumerable<T> expected, IEnumerable<T> actual)
        {
            return expected.Where(e => !actual.Contains(e));
        }        
    }
}
